package com.elinkway.infinitemovies.ui.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.elinkway.infinitemovies.download.DownloadHelper;
import com.elinkway.infinitemovies.utils.LogUtils;
import com.elinkway.infinitemovies.utils.MoviesConstant;
import com.elinkway.infinitemovies.utils.Utils;
import com.le123.ysdq.R;
import com.umeng.analytics.MobclickAgent;

import java.io.File;
import java.io.RandomAccessFile;
import java.util.List;

/**
 * Created by zhaopeipei on 2016/5/24.
 */
public class DownloadSelectActivity  extends BaseSecondaryActivity implements View.OnClickListener {

    private static final String TAG = "downloadSelectActivity";
    private static final int RQUEST_CODE_DOWNLOAD = 1;
    private RelativeLayout mRelativeOutsideStorage;
    private RelativeLayout mRelativeInsideStorage;
    private ImageView mOutsideStorage;
    private ImageView mInsideStorage;
    private String mCurrentPathSelect;
    private String mCurrentGender;
    private RelativeLayout mWhiteLine;
    private List<String> mPathList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download_modify);
        initActionBar();
        mLeftBackIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        mTitleTv.setText(getResources().getString(R.string.download_path_select));
        initView();
        setListener();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.user_message, menu);
        MenuItem item = menu.findItem(R.id.action_sure);
        View mView = MenuItemCompat.getActionView(item);
        mLeftBackIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(MoviesConstant.DOWNLOAD_PATH, mCurrentGender);
                setResult(RQUEST_CODE_DOWNLOAD, intent);
                finish();
            }
        });
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // TODO Auto-generated method stub
        if(item.getItemId() == android.R.id.home)
        {
            Intent intent = new Intent();
            intent.putExtra(MoviesConstant.DOWNLOAD_PATH, mCurrentGender);
            setResult(RQUEST_CODE_DOWNLOAD, intent);
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        mPathList = DownloadHelper.getStorageList();
        mWhiteLine = (RelativeLayout) findViewById(R.id.white_line);
        mRelativeInsideStorage = (RelativeLayout) findViewById(R.id.download_inside_sd_select);
        mRelativeOutsideStorage = (RelativeLayout) findViewById(R.id.download_outside_sd_select);
        mOutsideStorage = (ImageView) findViewById(R.id.iv_outside_storage_select);
        mInsideStorage = (ImageView) findViewById(R.id.iv_inside_storage_select);
        mRelativeOutsideStorage.setVisibility(View.VISIBLE);
        mOutsideStorage.setVisibility(View.VISIBLE);
        LogUtils.e(TAG, "DOWNLOAD_PATH_SELECT " + getIntent().getIntExtra(MoviesConstant.DOWNLOAD_PATH_SELECT, 1));
        if (getIntent().getIntExtra(MoviesConstant.DOWNLOAD_PATH_SELECT, 1) == 1) {
            mOutsideStorage.setVisibility(View.GONE);
            mRelativeOutsideStorage.setVisibility(View.GONE);
            mWhiteLine.setVisibility(View.GONE);
            mRelativeInsideStorage.setVisibility(View.VISIBLE);
            mInsideStorage.setVisibility(View.VISIBLE);
        }else{
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && !isCanWriteToSd()){
                mOutsideStorage.setVisibility(View.GONE);
                mRelativeOutsideStorage.setVisibility(View.GONE);
                mWhiteLine.setVisibility(View.GONE);
                mRelativeInsideStorage.setVisibility(View.VISIBLE);
                mInsideStorage.setVisibility(View.VISIBLE);
            }
        }
    }

    /**
     * 判断是否可写入sd卡
     * @return
     */
    private boolean isCanWriteToSd() {
        boolean isCan = false;
        if(mPathList.size() > 1){
            String path = mPathList.get(1).toString()+ "/"+Utils.getDownLoadFolder();
            //构建下载目录
            try {
                File file = new File(path);
                if (!file.exists())
                    file.mkdirs();
            } catch (Exception e) {
                e.printStackTrace();
            }
            File file = new File(path, "test");
            try {
                RandomAccessFile randomFile = new RandomAccessFile(file, "rw");
                isCan = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return isCan;
    }

    private void setListener() {
        mRelativeOutsideStorage.setOnClickListener(this);
        mRelativeInsideStorage.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        int gender = Integer.valueOf(TextUtils.isEmpty(getIntent().getStringExtra(MoviesConstant.DOWNLOAD_PATH))
                ? "0" : getIntent().getStringExtra(MoviesConstant.DOWNLOAD_PATH));
        genderShow(gender);
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.download_inside_sd_select:
                genderShow(0);
                finishAtNow();
                break;
            case R.id.download_outside_sd_select:
                genderShow(1);
                finishAtNow();
                break;
        }
    }

    public void finishAtNow() {
        Intent intent = new Intent();
        intent.putExtra(MoviesConstant.DOWNLOAD_PATH, mCurrentGender);
        setResult(RQUEST_CODE_DOWNLOAD, intent);
        finish();
    }


    public void genderShow(int gender) {
        if(1 == gender) {
            mOutsideStorage.setVisibility(View.VISIBLE);
            mInsideStorage.setVisibility(View.GONE);
            mCurrentPathSelect = "手机外接存储";
            mCurrentGender = "1";
        }else if(0 == gender) {
            mOutsideStorage.setVisibility(View.GONE);
            mInsideStorage.setVisibility(View.VISIBLE);
            mCurrentPathSelect = "手机内置存储";
            mCurrentGender = "0";
        }
    }

}
